package com.leonardo.beanme.fields.impl;

import javax.microedition.lcdui.ChoiceGroup;

import com.leonardo.beanme.fields.abs.ChoiceInput;

public final class BooleanInput extends ChoiceInput {
	
	private int trueIndex;
	private int falseIndex;
	private boolean lock;
	
	public BooleanInput() {
		super(ChoiceGroup.EXCLUSIVE);
		this.trueIndex = addOption("True");
		this.falseIndex = addOption("False");
		getGroup().setSelectedIndex(trueIndex, true);
	}
	
	public Object getValue() {
		return new Boolean(getGroup().getSelectedIndex() == trueIndex);  
	}

	public void setValue(Object obj) {
		if(lock)	return;
		if(!(obj instanceof Boolean))
			throw new IllegalArgumentException();
		Boolean b = (Boolean) obj;
		int index = (b.booleanValue() ? trueIndex : falseIndex);
		getGroup().setSelectedIndex(index, true);
	}
	
	public void lock() {
		lock = true;
	}
	
	public void unlock() {
		lock = false;
	}
	
}
